'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 开服狂欢
 * @author by: vincentxiaoxu
 */

var async = require('async');
var moment = require('moment');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var dataApi = require('../../../libs/dataApi');
var configUtil = require('../../../util/configUtil');
var PlayerManager = require('../../../libs/playerManager');
var openServerConf = require('../../../config/openServerConf.js');
var rewardService = require('../../../services/rewardService');
var constantUtil = require('../../../util/constantUtil');
var activityService = require('../../../services/activityService');
var actType = require('../../../consts/activityType.js');
var _ = require('lodash');
var carnivalType = require('../../../consts/carnivalType.js');
var taskFactory = require('../../../libs/taskFactory.js');
var open = require('../../../../config/development/open.json');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 开服狂欢列表
 */
Handler.prototype.list = function (msg, session, next) {
    var role_id = session.uid;
    var open_list = [];
    var copy_total_star = 0;
    var complete_list = [];

    // jshint maxstatements:100
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['activity', 'role', 'arena', 'expedition', 'build', 'task', 'copy', 'soldier', 'battle', 'bag', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var task_model = player_manager.task;
                var build_model = player_manager.build;
                var battle_model = player_manager.battle;
                var soldier_model = player_manager.soldier;
                var activity_model = player_manager.activity;

                complete_list = activity_model.getCarnivalReceive();
                // 副本星级总数
                var copy_model = player_manager.copy;
                var copy_list = dataApi.carbon_map.all();
                _.each(copy_list, function (copy_id, copy_key) {
                    copy_total_star += copy_model.getTotalStarNum(copy_key);
                });

                open_list = openServerConf.getOpenServerList();
                _.each(open_list, function (value) {
                    var carnival_type = value.type;
                    switch (carnival_type) {
                    case carnivalType.HERO_LV:
                        {
                            // 英雄等级达到N级
                            if (role_model.getLv() >= value.value1 && value.state == 2) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.COPY_STAR:
                        {
                            // 副本星级累积到N星
                            if (copy_total_star >= value.value1 && value.state == 2) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.NEXUS_LV:
                        {
                            // 主城等级达到N级
                            if (build_model.getBuild(20101).lv >= value.value1 && value.state == 2) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.BATTLE_NUM:
                        {
                            // 掠夺次数达到N次
                            var battle_num = task_model.getStaticList(217).battle_success_num || 0;

                            if (battle_num >= value.value1 && value.state == 2) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.RANK:
                        {
                            // 官阶达到N阶
                            if (battle_model.getNoble() >= value.value1 && value.state == 2) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.DEFENSE:
                        {
                            // 成功防守N次
                            var defense_num = task_model.getStaticList(218).battle_defense_num || 0;

                            if (defense_num >= value.value1 && value.state == 2) {
                                value.state = 1;
                            }

                            break;
                        }
                    case carnivalType.SOLDIER_STAR:
                        {
                            // 士兵星级达到N星有多少个
                            if (soldier_model.getSoldierStarNum1(value.value2) >= value.value1 && value.state == 2) {
                                value.state = 1;
                            }
                            break;
                        }
                    }
                });

                player_manager.save();
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    open_list: open_list,
                    open_server_time: open.open_time,
                    complete_list: complete_list
                }
            });
        }
    );
};

/**
 * 开服狂欢领取奖励
 */
Handler.prototype.receive = function (msg, session, next) {
    var role_id = session.uid;
    var carnival_id = msg.carnival_id;

    if (!carnival_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }
    var open_list = [];
    var copy_total_star = 0;
    var reward_list = [];
    var complete_list = [];

    // jshint maxstatements:100
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['activity', 'role', 'arena', 'expedition', 'build', 'task', 'copy', 'soldier', 'soldier', 'battle', 'tech', 'bag', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var task_model = player_manager.task;
                var build_model = player_manager.build;
                var battle_model = player_manager.battle;
                var soldier_model = player_manager.soldier;
                var activity_model = player_manager.activity;

                // 奖励是是否领取过
                if (!activity_model.isCarnivalReceive(carnival_id)) {
                    return next(null, {
                        code: Code.CARNIVAL_RECEIVE_NOT
                    });
                }
                complete_list = activity_model.getCarnivalReceive();

                // 副本星级总数
                var copy_model = player_manager.copy;
                var copy_list = dataApi.carbon_map.all();
                _.each(copy_list, function (copy_id, copy_key) {
                    copy_total_star += copy_model.getTotalStarNum(copy_key);
                });

                open_list = openServerConf.getOpenServerList();
                _.each(open_list, function (value) {
                    var carnival_type = value.type;
                    value.state = 0;
                    switch (carnival_type) {
                    case carnivalType.HERO_LV:
                        {
                            // 英雄等级达到N级
                            if (role_model.getLv() >= value.value1) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.COPY_STAR:
                        {
                            // 副本星级累积到N星
                            if (copy_total_star >= value.value1) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.NEXUS_LV:
                        {
                            // 主城等级达到N级
                            if (build_model.getBuild(20101).lv >= value.value1) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.BATTLE_NUM:
                        {
                            // 掠夺次数达到N次
                            var battle_num = task_model.getStaticList(217).battle_success_num || 0;

                            if (battle_num >= value.value1) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.RANK:
                        {
                            // 官阶达到N阶
                            if (battle_model.getNoble() >= value.value1) {
                                value.state = 1;
                            }
                            break;
                        }
                    case carnivalType.DEFENSE:
                        {
                            // 成功防守N次
                            var defense_num = task_model.getStaticList(218).battle_defense_num || 0;

                            if (defense_num >= value.value1) {
                                value.state = 1;
                            }

                            break;
                        }
                    case carnivalType.SOLDIER_STAR:
                        {
                            // 士兵星级达到N星
                            if (soldier_model.getSoldierStarNum1(value.value2) >= value.value1) {
                                value.state = 1;
                            }
                            break;
                        }
                    }
                });

                // 任务是否完成
                var state = false;
                _.each(open_list, function (carnival_value) {
                    if (carnival_id == carnival_value.id) {
                        if (carnival_value.state == 1) {
                            state = true;
                        }
                        return false;
                    }
                });

                if (!state) {
                    return next(null, {
                        code: Code.CARNIVAL_TASK_NOT
                    });
                }

                // 加入领取记录
                activity_model.addCarnivalReceive(carnival_id);

                // 发放奖励
                reward_list = openServerConf.getOpenServerRewardList(carnival_id);
                if (!reward_list) {
                    return next(null, {
                        code: Code.REWARD_ERROR
                    });
                }
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_ACT_OPEN_SVR_CARNIVAL, operationType.OPR_ACT_OPEN_SVR_CARNIVAL);

                // 重新推送7天完成状态
                activityService.push(role_id, actType.CHECK, player_manager);
                player_manager.save();
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list,
                    complete_list: complete_list

                }
            });
        }
    );
};
